package com.bocai.eelly.activitys;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.FindFactoryAdp;
import com.bocai.eelly.bean.FactoryListBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.bocai.eelly.views.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 *搜索闲置工厂
 */
public class SearchFactoryActivity extends BaseActivity implements XListView.IXListViewListener,AdapterView.OnItemClickListener {
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.search_cancle)
    Button searchCancle;
    @Bind(R.id.search_list)
    XListView searchList;

    private int page=1;
    private List<FactoryListBean.Data> dataEntities=new ArrayList<>();
    private FindFactoryAdp findFactoryAdp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_search, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findFactoryAdp=new FindFactoryAdp(this,dataEntities);
        searchList.setAdapter(findFactoryAdp);
        initEvent();
    }

    private void initEvent() {
        searchList.setXListViewListener(this);
        searchList.setPullLoadEnable(false);
        searchList.setPullRefreshEnable(true);
        searchList.setOnItemClickListener(this);
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    page=1;
                    dataEntities.clear();
                    searchList.setPullLoadEnable(false);
                    getFactory();
                }
                return false;
            }
        });

    }

    private void getFactory() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().getFactoryList(timeline,sign,Utils.getUserId(this,"id"),etSearch.getText().toString(),"","",page+"",Utils.PAGESIZE+"")
                .compose(this.<FactoryListBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FactoryListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        searchList.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showShort(SearchFactoryActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(FactoryListBean factoryListBean) {
                        searchList.stopRefresh();
                        Loading.dismiss();
                        L.d(factoryListBean.getMsg());
                        if (Utils.checkResultCode(factoryListBean.getReturn_code()))
                        {
                            dataEntities.addAll(factoryListBean.getData());
                            findFactoryAdp.notifyDataSetChanged();
                            if (factoryListBean.getData().size() < Utils.PAGESIZE) {
                                searchList.setPullLoadEnable(false);
                            }else{
                                searchList.setPullLoadEnable(true);
                            }
                        }else
                        {
                            T.showLong(SearchFactoryActivity.this,factoryListBean.getMsg());
                        }
                    }
                });
    }


    @Override
    public void onRefresh() {
        page=1;
        dataEntities.clear();
        searchList.setPullLoadEnable(false);
        getFactory();
    }

    @Override
    public void onLoadMore() {
        page++;
        getFactory();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
